//
//  YHVisitorView.swift
//  XLWB
//
//  Created by key on 16/6/25.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit

class YHVisitorView: UIView {

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.backgroundColor = RGB(237, green: 237, blue: 237)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //MARK: - 个控制器设置数据
    func setDataToView(text : String?, imgName : String?) {
        
        if let desText = text, let houseImgName = imgName {
            
            //不为空 表示不是首页
            self.smalliconImg.hidden = true
            
            self.describeLabel.text = desText
            
            houseImg.image = UIImage(named: houseImgName)
            
        
        }else {
            
            //为空 表示是首页 圆圈转起来
            let basic = CABasicAnimation(keyPath: "transform.rotation")
         
            basic.toValue = M_PI * 2
            
            basic.duration = 12
            
            basic.repeatCount = MAXFLOAT
            
            basic.removedOnCompletion = false
            
            smalliconImg.layer.addAnimation(basic, forKey: nil)
        }
    }
    
    
    //MARK: - 布局子控件
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        //添加
        addSubview(smalliconImg)
        addSubview(maskSmalliconImg)
        addSubview(houseImg)
        addSubview(describeLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        
        //设置约束
        setLayoutView()
    }
    
    
    //MARK: - 懒加载图片框 和 登陆 注册按钮
    
    //圆圈 visitordiscover_feed_image_smallicon
    private lazy var smalliconImg : UIImageView = {
        
       let imgV = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return imgV
        
    }()
    
    //遮罩 visitordiscover_feed_mask_smallicon
    private lazy var maskSmalliconImg : UIImageView = {
        
       let imgV = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return imgV

    }()
    
    //设置房子图标 visitordiscover_feed_image_house
    private lazy var houseImg : UIImageView = {
       
        let imgV = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return imgV
        
    }()
    
    //设置文字信息
    private lazy var describeLabel : UILabel = {
        
        let label = UILabel()
        
        label.textAlignment = .Center
        
        label.font = UIFont.systemFontOfSize(14)
        
        label.numberOfLines = 0
        
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        
        label.textColor = themeColor
        
        return label
        
    }()
    
    //注册按钮 common_button_white_disable
    private lazy var registerBtn : UIButton = {
        
       let btn = UIButton()
    
        btn.setTitle("注册", forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.setTitleColor(themeColor, forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()
    
    //登陆按钮 common_button_white_disable
    private lazy var loginBtn : UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("登陆", forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.setTitleColor(themeColor, forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()
    
    //MARK: - 设置约束
    func setLayoutView() {
        
        //圆圈 visitordiscover_feed_image_smallicon
        smalliconImg.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
            
        }
        
        //遮罩 visitordiscover_feed_mask_smallicon
        maskSmalliconImg.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        
        //设置房子图标 visitordiscover_feed_image_house
        houseImg.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        
        //设置文字信息 
        describeLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(maskSmalliconImg.snp_bottom).offset(16)
            make.width.equalTo(224)
            make.height.equalTo(36)
            make.centerX.equalTo(self)
            
        }
        
        //登陆按钮 common_button_white_disable
        loginBtn.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(describeLabel.snp_bottom).offset(16)
            make.leading.equalTo(describeLabel)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        
        //注册按钮
        registerBtn.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(describeLabel.snp_bottom).offset(16)
            make.trailing.equalTo(describeLabel)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        
    }
    
    
    
    //MARK: - 登陆 注册按钮
    
    var visitorBag : (() -> ())?
    
    @objc private func loginClick() {
        
        //调用
        self.visitorBag?()
        
        
    }
    


}
